package com.bjbn.controller;

import com.bjbn.service.Webapi4BmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * T3能管平台接口控制器
 *
 * @author bjbn
 */
@RestController
@RequestMapping("/CAN/BMS/EnergyManagementSystem/V1/api/")
@Tag(name = "T3能管平台接口", description = "提供T3能管平台相关的接口转发服务")
public class BmsController {

    @Autowired
    private Webapi4BmsService webapi4BmsService;

    /**
     * 获取Token
     *
     * @param timeout 超时时间（分钟），默认为10分钟
     * @return 响应结果
     */
    @PostMapping("Token")
    @Operation(summary = "获取能管平台Token", description = "获取访问能管平台接口的Token，默认超时时间为10分钟")
    public Mono<ResponseEntity<Map<String, Object>>> getToken(@RequestParam(defaultValue = "10") Integer timeout) {
        return webapi4BmsService.getBmsToken(timeout)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    // =============================== 3.1.2 查询节点能耗

    /**
     * 获取维度列表
     *
     * @return 响应结果
     */
    @GetMapping("Cube")
    @Operation(summary = "获取维度列表", description = "获取能管平台的维度列表数据")
    public Mono<ResponseEntity<Map<String, Object>>> getCubeList() {
        return webapi4BmsService.getCubeList()
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取指定维度的树结构
     *
     * @param CubeCode 维度编号
     * @return 响应结果
     */
    @GetMapping("Dimension/CubeCode")
    @Operation(summary = "获取指定维度的树结构", description = "根据维度编号获取树结构数据")
    public Mono<ResponseEntity<Map<String, Object>>> getDimensionTree(@RequestParam String CubeCode) {
        return webapi4BmsService.getDimensionTree(CubeCode)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取维度节点关联仪表信息
     *
     * @param CubeCode 维度编号
     * @return 响应结果
     */
    @GetMapping("DimensionMeter/CubeCode")
    @Operation(summary = "获取维度节点关联仪表信息", description = "根据维度编号获取关联的仪表信息")
    public Mono<ResponseEntity<Map<String, Object>>> getDimensionMeter(@RequestParam String CubeCode) {
        return webapi4BmsService.getDimensionMeter(CubeCode)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取测量量类型
     *
     * @return 响应结果
     */
    @GetMapping("Basic/Measurand")
    @Operation(summary = "获取测量量类型", description = "获取能管平台的测量量类型数据")
    public Mono<ResponseEntity<Map<String, Object>>> getMeasurandTypes() {
        return webapi4BmsService.getMeasurandTypes()
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取能源类型转换率
     *
     * @return 响应结果
     */
    @GetMapping("Basic/EnergyRatio")
    @Operation(summary = "获取能源类型转换率", description = "获取能管平台的能源类型转换率数据")
    public Mono<ResponseEntity<Map<String, Object>>> getEnergyRatios() {
        return webapi4BmsService.getEnergyRatios()
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取维度节点指定小时/天/月/年时间段能耗数据
     *
     * @param requestBody 请求参数
     * @return 响应结果
     */
    @PostMapping("EnergyConsumption/Dimension/MultFuncDetail")
    @Operation(summary = "获取维度节点能耗数据", description = "获取维度节点指定小时/天/月/年时间段能耗数据")
    public Mono<ResponseEntity<Map<String, Object>>> getDimensionEnergyConsumption(@RequestBody Map<String, Object> requestBody) {
        return webapi4BmsService.getDimensionEnergyConsumption(requestBody)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取日/月/年时段的定额数据
     *
     * @param requestBody 请求参数
     * @return 响应结果
     */
    @PostMapping("QuotaDataManage/GetNodeQuotaData")
    @Operation(summary = "获取节点定额数据", description = "获取日/月/年时段的定额数据")
    public Mono<ResponseEntity<Map<String, Object>>> getNodeQuotaData(@RequestBody Map<String, Object> requestBody) {
        return webapi4BmsService.getNodeQuotaData(requestBody)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    // =============================== 3.1.3 查询仪表能耗数据及表底值、设备状态

    /**
     * 获取所有设备ID及属性信息
     *
     * @return 响应结果
     */
    @GetMapping("Basic/Meter")
    @Operation(summary = "获取所有设备信息", description = "获取所有设备ID及属性信息")
    public Mono<ResponseEntity<Map<String, Object>>> getAllMeters() {
        return webapi4BmsService.getAllMeters()
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取单个设备下的所有测点数据
     *
     * @param meterId 设备ID
     * @return 响应结果
     */
    @GetMapping("Basic/Meter/MeasuringDeviceID")
    @Operation(summary = "获取设备测点数据", description = "获取单个设备下的所有测点数据")
    public Mono<ResponseEntity<Map<String, Object>>> getMeterPoints(@PathVariable String meterId) {
        return webapi4BmsService.getMeterPoints(meterId)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取多个AI遥测量实时值
     *
     * @param requestBody 请求参数（包含设备ID列表）
     * @return 响应结果
     */
    @PostMapping("IOServer/AIRange")
    @Operation(summary = "获取多个AI遥测量实时值", description = "获取多个设备的AI遥测量实时值")
    public Mono<ResponseEntity<Map<String, Object>>> getAIRange(@RequestBody Map<String, Object> requestBody) {
        return webapi4BmsService.getAIRange(requestBody)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取设备AI遥测量历史值
     *
     * @param requestBody 请求参数
     * @return 响应结果
     */
    @PostMapping("IOServer/MeterAIHistoryValue")
    @Operation(summary = "获取设备AI遥测量历史值", description = "获取设备AI遥测量历史数据")
    public Mono<ResponseEntity<Map<String, Object>>> getMeterAIHistoryValue(@RequestBody Map<String, Object> requestBody) {
        return webapi4BmsService.getMeterAIHistoryValue(requestBody)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取DI点信息
     *
     * @return 响应结果
     */
    @GetMapping("WebSiteDomain/api/Basic/DI")
    @Operation(summary = "获取DI点信息", description = "获取所有DI点的基础信息")
    public Mono<ResponseEntity<Map<String, Object>>> getDIPoints() {
        return webapi4BmsService.getDIPoints()
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取多个DI遥信量实时值
     *
     * @param requestBody 请求参数
     * @return 响应结果
     */
    @PostMapping("IOServer/DIRange")
    @Operation(summary = "获取多个DI遥信量实时值", description = "获取多个设备的DI遥信量实时值")
    public Mono<ResponseEntity<Map<String, Object>>> getDIRange(@RequestBody Map<String, Object> requestBody) {
        return webapi4BmsService.getDIRange(requestBody)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取仪表每小时/天/月/年时间段能耗数据
     *
     * @param requestBody 请求参数
     * @return 响应结果
     */
    @PostMapping("EnergyConsumption/Meter/MultFuncDetail")
    @Operation(summary = "获取仪表能耗数据", description = "获取仪表每小时/天/月/年时间段能耗数据")
    public Mono<ResponseEntity<Map<String, Object>>> getMeterEnergyConsumption(@RequestBody Map<String, Object> requestBody) {
        return webapi4BmsService.getMeterEnergyConsumption(requestBody)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取ACC量的实时表底值
     *
     * @param requestBody 请求参数
     * @return 响应结果
     */
    @PostMapping("IOServer/ACCRangeWithInvalid")
    @Operation(summary = "获取ACC量实时表底值", description = "获取ACC量的实时表底值")
    public Mono<ResponseEntity<Map<String, Object>>> getACCRangeWithInvalid(@RequestBody Map<String, Object> requestBody) {
        return webapi4BmsService.getACCRangeWithInvalid(requestBody)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取ACC量的历史表底值
     *
     * @param requestBody 请求参数
     * @return 响应结果
     */
    @PostMapping("IOServer/AccHistoryValueByTimeType")
    @Operation(summary = "获取ACC量历史表底值", description = "获取ACC量的历史表底值")
    public Mono<ResponseEntity<Map<String, Object>>> getAccHistoryValueByTimeType(@RequestBody Map<String, Object> requestBody) {
        return webapi4BmsService.getAccHistoryValueByTimeType(requestBody)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取多个设备的通讯状态
     *
     * @param requestBody 请求参数（包含设备ID列表）
     * @return 响应结果
     */
    @PostMapping("IOServer/DevStatusRange")
    @Operation(summary = "获取设备通讯状态", description = "获取多个设备的通讯状态")
    public Mono<ResponseEntity<Map<String, Object>>> getDevStatusRange(@RequestBody Map<String, Object> requestBody) {
        return webapi4BmsService.getDevStatusRange(requestBody)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }

    /**
     * 获取历史报警数据
     *
     * @param requestBody 请求参数
     * @return 响应结果
     */
    @PostMapping("Alarm/History")
    @Operation(summary = "获取历史报警数据", description = "获取指定条件下的历史报警数据")
    public Mono<ResponseEntity<Map<String, Object>>> getAlarmHistory(@RequestBody Map<String, Object> requestBody) {
        return webapi4BmsService.getAlarmHistory(requestBody)
                .map(ResponseEntity::ok)
                .defaultIfEmpty(ResponseEntity.badRequest().build());
    }
}